package org.darkforest.common.services.impl;

import org.darkforest.common.services.FestivalRemindService;
import org.darkforest.common.services.FestivalService;
import org.darkforest.common.services.MobileMsgService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.*;
import org.darkforest.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pc on 2017/5/22.
 */
@Service("remindService")
public class FestivalRemindServiceImpl implements FestivalRemindService {
    @Autowired
    private FestivalRemindMapper festivalRemindMapper;
    @Autowired
    private MemberRemindSettingMapper memberRemindSettingMapper;
    @Autowired
    private MemberMsgTplMapper memberMsgTplMapper;
    @Autowired
    private MobileMsgService mobileMsgService;
    @Autowired
    private MemberMapper  memberMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Override
    public void remindMsg() {
        FestivalRemind record = new FestivalRemind();
        record.setPageSwitch(false);
        record.setShowFlag("1");
        List<FestivalRemind> list= festivalRemindMapper.selectRemindListByCondition(record);
        List<Message> messages = new ArrayList<Message>();
        for(FestivalRemind remind:list){
            boolean contactsRemindSwtich = true;
            MemberRemindSetting memberRemindSetting =memberRemindSettingMapper.selectByPrimaryKey(remind.getCreaterId());
            if(memberRemindSetting!=null&&memberRemindSetting.getrContactsRemindSwtich().equals("0")){
                contactsRemindSwtich = false;
            }
            if(contactsRemindSwtich){
                MemberMsgTpl memberMsgTpl=memberMsgTplMapper.selectByPrimaryKey("contact_remind_notice");
                MemberWithBLOBs mb = memberMapper.selectByPrimaryKey(remind.getCreaterId());
                //发短信
                if(memberMsgTpl != null && memberMsgTpl.getMmtShortId()!=0&&memberMsgTpl.getMmtShortContent()!=null&&(mb.getMemberMobile()!=null&&!mb.getMemberMobile().equals(""))){
                    String[] str = new String[2];
                    str[0] =remind.getfName();
                    str[1] =remind.getfRemindName();
                    mobileMsgService.sendMobileMsg("",contextRepalce(memberMsgTpl.getMmtShortContent(),str));
                }
                //站内信
                if(memberMsgTpl != null && memberMsgTpl.getMmtMessageForced()!=0&&memberMsgTpl.getMmtMessageContent()!=null){
                    String[] str = new String[2];
                    str[0] =remind.getfName();
                    str[1] =remind.getfRemindName();
                    Message message = new Message();
                    message.setMessageParentId(0);
                    message.setMessageOpen(0);
                    message.setMessageState(0);
                    message.setMessageTitle(memberMsgTpl.getMmtName());
                    message.setMessageIsmore(0);
                    message.setMessageBody(contextRepalce(memberMsgTpl.getMmtShortContent(),str));
                    message.setMessageType(1);
                    message.setFromMemberId(0);
                    message.setFromMemberName("system");
                    message.setToMemberId(mb.getMemberId().toString());
                    message.setToMemberName(mb.getMemberName());
                    message.setMessageTime(DateUtil.unixTimeStampInt().intValue());
                    message.setMessageClass(memberMsgTpl.getMmtClass());
                    messages.add(message);
                }
            }

        }
        if(messages.size()>0)messageMapper.batchInsert(messages);
    }
    public String contextRepalce(String context,String []str){
        for (int i=0;i<str.length;i++){
            context = context.replace("{"+(i+1)+"}",str[i]);
        }
        return context;
    }
}
